#ifndef SERVER_H
#define SERVER_H

#include <QObject>
#include <QString>

class QWebSocketServer;
class QWebSocket;
class Server : public QObject
{
    Q_OBJECT;

private:
    Server(QObject *parent);
public:
    ~Server();
public:
    static void send(const QString &type, const QVariantMap &args);
    static bool isReady();
    static bool isListening() { return _instance != nullptr && _instance->server->isListening(); }
    static void init(QObject *parent);
signals:
    void message(const QString &type, const QVariantMap &args);
private:
    void _send(const QString &type, const QVariantMap &args);
    void _sendText(const QString &str);
    private slots:
    void onNewConnection();
signals:
    void _notifySend(QString msg);
    private slots:
    void _sendHelper(QString msg);
private:
    void onUnknownClientTextMessageRecieved(QWebSocket *client, QString message);
    void onClientTextMessageRecieved(QWebSocket *client, QString message);
    void registerNewClient(QWebSocket * client);
    void onClientDisconnected(QWebSocket * client);
private:
    QWebSocketServer *server = nullptr;

    QWebSocket *client = nullptr;
    static Server *_instance;
};

#endif // SERVER_H
